package com.baizhi.service.impl;

import com.baizhi.dao.VideoMapper;
import com.baizhi.dto.PageDTO;
import com.baizhi.entity.Video;
import com.baizhi.entity.VideoExample;
import com.baizhi.service.VideoService;
import com.baizhi.vo.VideoVO;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class VideoServiceImpl implements VideoService {
    @Resource
    private VideoMapper videoMapper;

    //查询所有视频数据
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public VideoVO queryAllPage(PageDTO pageDTO) {
        VideoVO videoVO = new VideoVO();
        int page = pageDTO.getPage();
        int pagSize = pageDTO.getPageSize();
        VideoExample videoExample = new VideoExample();
        videoExample.setOrderByClause("id asc");  //asc  desc
        RowBounds rowBounds = new RowBounds((page - 1) * pagSize, pagSize);
        List<Video> videoList = videoMapper.selectByExampleAndRowBounds(videoExample, rowBounds);
        videoVO.setRows(videoList);
        videoVO.setPage(page);
        int total = videoMapper.selectCount(new Video());
        videoVO.setTotal(total);
        return videoVO;
    }

    //添加视频
    @Override
    public Map<String, String> add(Video video) {
        Map<String,String> map = new HashMap<>();
        video.setCreateTime(new Date());
        int i = videoMapper.insert(video);
        if (i == 1) {
            map.put("message","添加成功");
            map.put("status","200");
        }else {
            map.put("message","添加失败");
            map.put("status","400");
        }
        return map;
    }

    //删除视频
    @Override
    public Map<String, String> delete(int id) {
        Map<String, String> map = new HashMap<>();
        int i = videoMapper.deleteByPrimaryKey(id);
        if (i == 1){
            map.put("message","删除成功");
            map.put("status","200");
        }else {
            map.put("message","删除失败");
            map.put("status","400");
        }
        return map;
    }

    //根据id查询视频
    @Override
    public Video queryById(int id) {
        return videoMapper.selectByPrimaryKey(id);
    }



    @Override
    public void update(Video video) {
        videoMapper.updateByPrimaryKeySelective(video);
    }
}
